package com.henry.learngui;

import com.henry.imgproc.MedianFilter;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

import java.awt.image.BufferedImage;

import static com.henry.imgproc.MedianFilterDemo.loadBufferedImageFromFile;

public class MedianFilterDisplay extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        String pathPrefix = "src/main/resources/ImageProcess/";
        String inputFileName = "ship.png";

        //String outputFileName = "ship_fix.png";

        BufferedImage original = loadBufferedImageFromFile(pathPrefix + inputFileName);

        BufferedImage result = null;

        if (original != null) {
            System.out.println("Image rows: " + original.getHeight());
            System.out.println("Image cols: " + original.getWidth());
            try {
                result = MedianFilter.medianFilter(original,3,3);}
            catch (Exception e) {
                System.out.println(e.toString());
            }
        }
        else {
            System.out.println("The original image is null!");
        }

        FlowPane root = new FlowPane();
        root.setAlignment(Pos.CENTER);

        if (result != null) {
            ImageView imageView = new ImageView();
            Image image = SwingFXUtils.toFXImage(result, null);
            imageView.setImage(image);
            root.getChildren().add(imageView);
        } else {
            System.out.println("The result image is null!");
        }

        Scene scene = new Scene(root, 512, 512);
        stage.setScene(scene);
        stage.setTitle(inputFileName);

        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
